﻿using System;
using System.IO;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Modules.QuickDownload.Components;

namespace ASPNETVN.PORTAL.Modules.QuickDownload.Controls
{
    public partial class UploadDocument : AddEditItemControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                if (this.ItemID > 0)
                {
                    Item document = Item.Load(this.ItemID);
                    txtFilePath.Text = document.FilePath;
                    rblLinkType.SelectedValue = document.LinkType;
                }
                if (rblLinkType.SelectedItem.Value == "INTERNAL")
                {
                    uploadFile.Enabled = true;
                    txtFilePath.Enabled = false;
                }
                else
                {
                    uploadFile.Enabled = false;
                    txtFilePath.Enabled = true;
                }
            }
            
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.Page.IsValid)
            {
                if (this.ItemID > 0)
                {
                    Item document = Item.Load(this.ItemID);
                    if (rblLinkType.SelectedValue == "INTERNAL")
                    {
                        if (this.uploadFile.HasFile)
                        {
                            lblMessage.Text = "";
                            string uploadPath = this.Settings["UploadPath"].ToString();
                            string physicalDirectory = this.Server.MapPath("~/" + uploadPath + "/Documents/" + this.ModuleID);
                            // Tạo thư mục để chứa tập tin.
                            if (Directory.Exists(physicalDirectory) == false)
                            {
                                Directory.CreateDirectory(physicalDirectory);
                            }

                            string physicalPath = physicalDirectory + "\\" + this.uploadFile.FileName;
                            int i = 1;
                            while (File.Exists(physicalPath))
                            {
                                physicalPath = physicalDirectory + "\\" + i + "_" + this.uploadFile.FileName;
                                i++;
                            }
                            // Lưu tập tin.
                            this.uploadFile.SaveAs(physicalPath);

                            document.FilePath = this.ResolveClientUrl("~/" + uploadPath + "/Documents/" + this.ModuleID + "/" + physicalPath.Substring(physicalPath.LastIndexOf('\\') + 1));
                            // Internal link.
                            document.LinkType = "INTERNAL";
                            document.FileType = uploadFile.PostedFile.ContentType;
                        }
                        else
                        {
                            lblMessage.Text = "Chưa chọn tập tin";
                            return;
                        }
                    }
                    else
                    {
                        if (this.txtFilePath.Text.Length > 0)
                        {
                            lblMessage.Text = "";
                            document.FilePath = this.txtFilePath.Text;
                            // External link.
                            document.LinkType = "EXTERNAL";
                        }
                        else
                        {
                            lblMessage.Text = "Chưa chọn liên kết đến tập tin";
                            return;
                        }
                    }
                    
                    document.ID = this.ItemID;
                    document.Update();

                    this.ReturnToReferrerPage();
                }
            }
        }

        protected void rblLinkType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rblLinkType.SelectedItem.Value == "INTERNAL")
            {
                uploadFile.Enabled = true;
                txtFilePath.Enabled = false;
            }
            else
            {
                uploadFile.Enabled = false;
                txtFilePath.Enabled = true;
            }
        }
    }
}